import { _decorator, Button, color, Label, Node, ProgressBar, ScrollView } from 'cc';
import { Ab_DataTransfer_bA } from '../../../A-FRAME/A-LIB/lib.b.data';
import { ScrollList } from '../../../A-FRAME/component/scroll.list';
import { Ab_BasePopUp_bA } from '../../../A-FRAME/component/ui.pop-up';
import Ab_Config_bA from '../../manager/config.manager';
import Ab_GameDC_bA from '../../manager/data.game.center';
import Ab_GameAssets_bA from '../../manager/game.assets';
import type { Ab_Popup_bA } from '../page/popup';
const { ccclass, property } = _decorator;

@ccclass('Ab_Tast_bA')
export class Ab_Tast_bA extends Ab_BasePopUp_bA<typeof Ab_Popup_bA, null> {

	@property(ScrollList)
	private Ab_scroll_list_bA: ScrollList = null;
	@property(ScrollView)
	private Ab_scroll_view_bA: ScrollView = null;

	protected Ab_default_return_bA: null;

	protected Ab_initAfterOnLoad_bA(){
		this.Ab_renderLocalUI_bA();
		this.Ab_scroll_list_bA.onRenderItem(this.Ab_renderItem_bA.bind(this));
	}

	protected Ab__click_event__bA: { [name: string]: (this: Ab_Tast_bA, button: Button) => void; } = {
		close(btn){
			btn.interactable = false;
			Ab_BasePopUp_bA.Ab_jelly_leave_bA(this.node).then(()=>{
				this.Ab_close_bA(null);
				btn.interactable = true;
				Ab_GameDC_bA.Ab_Trigger_bA.Ab_HILIT_DIAMOND_bA = false;
			});
		},
		remove(btn){
			btn.interactable = false;
			this.Ab_renderList_bA('remove');
		},
		round(btn){
			btn.interactable = false;
			this.Ab_renderList_bA('round');
		},
		reap(btn){
			btn.interactable = false;
			this.Ab_reapTask_bA(btn.node.parent);
			btn.interactable = true;
		}
	}

	protected Ab_listening_bA(){
		let list = [];

		let [hint_round, hint_remove] = ['round', 'remove'].map(name=>this.node.getChildByName('hint-'+name));
		let [lbl_round, lbl_remove] = ['round', 'remove'].map(name=>this.node.getChildByName('lbl-'+name).getComponent(Label));

		list.push(
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_User_bA, 'Ab_task_round_bA', ()=>{
				let count = Ab_Config_bA.Ab_taskRound_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('round', el.round) && el.round < Ab_GameDC_bA.Ab_User_bA.Ab_round_bA).length;
				hint_round.active = count>0;
				lbl_round.string = count>0 ? String(count) : '';
			}, false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_User_bA, 'Ab_task_remove_bA', ()=>{
				let count = Ab_Config_bA.Ab_taskRemove_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('remove', el.id) && el.count <= Ab_GameDC_bA.Ab_User_bA.Ab_remove_count_bA).length;
				hint_remove.active = count>0;
				lbl_remove.string = count>0 ? String(count) : '';
			}, false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_User_bA, 'Ab_round_bA', ()=>{
				let count = Ab_Config_bA.Ab_taskRound_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('round', el.round) && el.round < Ab_GameDC_bA.Ab_User_bA.Ab_round_bA).length;
				hint_round.active = count>0;
				lbl_round.string = count>0 ? String(count) : '';
			}, false),
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_User_bA, 'Ab_remove_count_bA', ()=>{
				let count = Ab_Config_bA.Ab_taskRemove_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('remove', el.id) && el.count <= Ab_GameDC_bA.Ab_User_bA.Ab_remove_count_bA).length;
				hint_remove.active = count>0;
				lbl_remove.string = count>0 ? String(count) : '';
			}, false)
		);

		let count_round = Ab_Config_bA.Ab_taskRound_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('round', el.round) && el.round < Ab_GameDC_bA.Ab_User_bA.Ab_round_bA).length;
		hint_round.active = count_round>0;
		lbl_round.string = count_round>0 ? String(count_round) : '';
		let count_remove = Ab_Config_bA.Ab_taskRemove_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('remove', el.id) && el.count <= Ab_GameDC_bA.Ab_User_bA.Ab_remove_count_bA).length;
		hint_remove.active = count_remove>0;
		lbl_remove.string = count_remove>0 ? String(count_remove) : '';

		return list;
	}

	public Ab_onShow_bA(){
		Ab_GameDC_bA.Ab_Trigger_bA.Ab_HILIT_DIAMOND_bA = true;
		this.Ab_renderList_bA('round');
		Ab_BasePopUp_bA.Ab_jelly_enter_bA(this.node);
	}

	private async Ab_reapTask_bA(item: Node){

		let data: any;
		if(this.Ab_render_type_bA=='round') data = Ab_Config_bA.Ab_taskRound_bA.find(el=>el.round==Number(item.name));
		else data = Ab_Config_bA.Ab_taskRemove_bA.find(el=>el.id==Number(item.name));

		await this.Ab_pop_bA.Ab_FruitDiamond_bA(data.diamond);

		Ab_GameDC_bA.Ab_recordTask_bA(this.Ab_render_type_bA, item.name);
		this.Ab_renderItem_bA(item, data, this.Ab_scroll_list_bA.NodeCache);
	}

	private Ab_renderLocalUI_bA(){
		(<const>[
			['round', 40],
			['remove', 41]
		]).forEach(([name, idx])=>this.Ab___btn_map___bA.get(name).getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(idx));

		this.Ab_scroll_view_bA.content.children.forEach(node=>{
			node.getChildByName('lbl-simple').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(1);
			node.getChildByName('lbl-hard').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(2);
			node.getChildByName('lbl-done').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(43);
			node.getChildByName('lbl-reap').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(44);
			abi.Ab_cc_bA.Ab_skinPeeler_bA(node.getChildByName('underway'), Ab_GameAssets_bA.Ab_getLangTexture_bA(5));
		});
	}

	private Ab_render_type_bA: 'round' | 'remove' = 'round';
	private Ab_renderList_bA(type: 'round' | 'remove'){
		this.Ab_render_type_bA = type;
		if(type=='round') this.Ab_scroll_list_bA.setDataList(Ab_Config_bA.Ab_taskRound_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('round', el.round)), 'round');
		else this.Ab_scroll_list_bA.setDataList(Ab_Config_bA.Ab_taskRemove_bA.filter(el=>!Ab_GameDC_bA.Ab_checkTask_bA('remove', el.id)), 'id');

		this.Ab_scroll_view_bA.scrollToTop(0.05);

		['round', 'remove'].forEach(name=>{
			this.Ab___btn_map___bA.get(name).interactable = name!=type;
			this.Ab___btn_map___bA.get(name).getComponent(Label).color = name==type ? color(168, 59, 20) : color(48, 107, 64);
			this.node.getChildByName('background-'+name).active = name==type;
		});
	}

	private Ab_renderItem_bA( node: Node, data: any, node_cache: Map<Node, __TaskNodeCache__> ){
		let cache = node_cache.get(node);
		if(!cache){
			cache = this.Ab_createCache_bA(node);
			node_cache.set(node, cache);
		}

		[
			cache.tag_simple,
			cache.tag_hard,
			cache.lbl_simple.node,
			cache.lbl_hard.node,
			cache.lbl_desc_round.node,
		].forEach(n=>n.active = this.Ab_render_type_bA=='round');

		[
			cache.lbl_desc_remove.node,
		].forEach(n=>n.active = this.Ab_render_type_bA=='remove');

		if(this.Ab_render_type_bA=='round') this.Ab_renderRoundItem_bA(cache, data);
		else this.Ab_renderRemoveItem_bA(cache, data);
	};

	private Ab_renderRoundItem_bA(cache: __TaskNodeCache__, data: typeof Ab_Config_bA.Ab_taskRound_bA[number]){
		cache.tag_simple.active = !data.hard;
		cache.tag_hard.active = data.hard;
		cache.lbl_simple.node.active = !data.hard;
		cache.lbl_hard.node.active = data.hard;
		cache.lbl_desc_round.string = Ab_GameAssets_bA.Ab_getLangText_bA(42, data.round);

		cache.lbl_diamond.string = String(data.diamond);

		let round = Math.min(Ab_GameDC_bA.Ab_User_bA.Ab_round_bA-1, data.round);
		let progress = Math.max(0, Math.min(1, round/data.round));

		cache.bar.progress = progress;
		cache.lbl_progress.string = `${round} / ${data.round}`;

		let done = Ab_GameDC_bA.Ab_checkTask_bA('round', data.round);
		let full = progress>=1;
		cache.btn_done.node.active = done;
		cache.lbl_done.node.active = done;
		cache.node_underway.active = !done && !full;
		cache.btn_reap.node.active = !done && full;
		cache.lbl_reap.node.active = !done && full;
	}

	private Ab_renderRemoveItem_bA(cache: __TaskNodeCache__, data: typeof Ab_Config_bA.Ab_taskRemove_bA[number]){
		cache.lbl_desc_remove.string = Ab_GameAssets_bA.Ab_getLangText_bA(45, data.count);
		cache.lbl_diamond.string = String(data.diamond);

		let count = Math.min(Ab_GameDC_bA.Ab_User_bA.Ab_remove_count_bA, data.count);
		let progress = Math.max(0, Math.min(1, count/data.count));

		cache.bar.progress = progress;
		cache.lbl_progress.string = `${count} / ${data.count}`;

		let done = Ab_GameDC_bA.Ab_checkTask_bA('remove', data.id);
		let full = progress>=1;

		cache.btn_done.node.active = done;
		cache.lbl_done.node.active = done;
		cache.node_underway.active = !done && !full;
		cache.btn_reap.node.active = !done && full;
		cache.lbl_reap.node.active = !done && full;
	}

	private Ab_createCache_bA(item_node: Node){
		let cache: __TaskNodeCache__ = {
			tag_simple: item_node.getChildByName('tag-simple'),
			tag_hard: item_node.getChildByName('tag-hard'),
			btn_done: item_node.getChildByName('done').getComponent(Button),
			node_underway: item_node.getChildByName('underway'),
			btn_reap: item_node.getChildByName('reap').getComponent(Button),
			bar: item_node.getChildByName('bar').getComponent(ProgressBar),
			lbl_simple: item_node.getChildByName('lbl-simple').getComponent(Label),
			lbl_hard: item_node.getChildByName('lbl-hard').getComponent(Label),
			lbl_progress: item_node.getChildByName('lbl-progress').getComponent(Label),
			lbl_reap: item_node.getChildByName('lbl-reap').getComponent(Label),
			lbl_done: item_node.getChildByName('lbl-done').getComponent(Label),
			lbl_desc_round: item_node.getChildByName('lbl-desc-round').getComponent(Label),
			lbl_desc_remove: item_node.getChildByName('lbl-desc-remove').getComponent(Label),
			lbl_diamond: item_node.getChildByName('lbl-diamond').getComponent(Label),
		};
		return cache;
	}
}

type __TaskNodeCache__ = {
	tag_simple: Node;
	tag_hard: Node;
	node_underway: Node;
	btn_done: Button;
	btn_reap: Button;
	bar: ProgressBar;
	lbl_simple: Label;
	lbl_hard: Label;
	lbl_progress: Label;
	lbl_reap: Label;
	lbl_done: Label;
	lbl_desc_round: Label;
	lbl_desc_remove: Label;
	lbl_diamond: Label;
}

